from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from django.utils.decorators import method_decorator
from django.views import View
import json
from .models import UserWebpage

@method_decorator(csrf_exempt, name='dispatch')
class UserWebpageCreateView(View):
    def post(self, request):
        try:
            # 确保请求体以 UTF-8 解码
            data = json.loads(request.body.decode('utf-8'))
            user = data.get('user')
            content = data.get('content')
            
            if not user or not content:
                return JsonResponse({
                    'success': False,
                    'message': '用户和内容字段不能为空'
                }, status=400, json_dumps_params={'ensure_ascii': False})
            
            existing_record = UserWebpage.objects.filter(user=user).first()
            
            if existing_record:
                existing_record.content = content
                existing_record.save()
                return JsonResponse({
                    'success': True,
                    'message': '用户内容已更新',
                    'id': existing_record.id,
                    'action': 'updated'
                }, json_dumps_params={'ensure_ascii': False})
            else:
                new_record = UserWebpage.objects.create(
                    user=user,
                    content=content
                )
                return JsonResponse({
                    'success': True,
                    'message': '用户内容已创建',
                    'id': new_record.id,
                    'action': 'created'
                }, json_dumps_params={'ensure_ascii': False})
                
        except json.JSONDecodeError:
            return JsonResponse({
                'success': False,
                'message': 'JSON 格式错误'
            }, status=400, json_dumps_params={'ensure_ascii': False})
        except Exception as e:
            return JsonResponse({
                'success': False,
                'message': f'服务器错误: {str(e)}'
            }, status=500, json_dumps_params={'ensure_ascii': False})

class UserWebpageDetailView(View):
    def get(self, request, user_id):
        try:
            record = UserWebpage.objects.filter(user=user_id).first()
            
            if not record:
                return JsonResponse({
                    'success': False,
                    'message': '未找到该用户的记录'
                }, status=404, json_dumps_params={'ensure_ascii': False})
            
            return JsonResponse({
                'success': True,
                'data': {
                    'id': record.id,
                    'user': record.user,
                    'content': record.content
                }
            }, json_dumps_params={'ensure_ascii': False})
            
        except Exception as e:
            return JsonResponse({
                'success': False,
                'message': f'服务器错误: {str(e)}'
            }, status=500, json_dumps_params={'ensure_ascii': False})
